require('./user.less')
require('../../assets/css/index.less')
require('../../assets/fonts/iconfont.css')
require('../../lib/weui/weui.css');
let weui = require('../../lib/weui/weui.js');

const dom = require('../../utils/dom');
const https = require('../../utils/https');
const local = require('../../utils/local');

dom.ready(function() {
    let imgUrl;
    dom.addevent("#fileBtn", function() {
        let file = this.files[0];
        let fd = new FormData();
        fd.append("file", file);
        https.post("/api/circles/uploadImg", fd, function(res) {
            console.log(res);
            let { errno, data } = res;

            if (errno == 0) {
                console.log(111);
                dom.gete("img").src = 'http://fitness.h5.itsource.cn:3701' + data.url;
                console.log(res.data.url);
                imgUrl = res.data.url;
            }
        })
        console.log(fd.get('file'));
        console.log(imgUrl);
    }, "change")


    dom.addevent("#picBtn", function() {
        dom.gete("#fileBtn").click();

    })


    dom.addevent("i", function() {
        location.href = "my.html"
    })


    dom.addevent('#sexBtn', function() {
        weui.picker([{
                label: '男',
                value: 0
            },
            {
                label: '女',
                value: 1
            }
        ], {
            onConfirm: function(result) {

                dom.gete('#sexTxt').innerHTML = result[0].label;
            },
            title: '请选择性别'
        });
    })



    dom.addevent('#birBtn', function() {
        weui.datePicker({
            start: 1950,
            end: new Date().getFullYear(),
            onConfirm: function(result) {

                let birDate = result.map(v => dom.formatDate(v.value)).join('-')

                dom.gete('#birTxt').innerHTML = birDate
            },
            title: '请选择生日'
        });
    })




    let proviceData;

    dom.addevent("#probtn", function() {

        https.get('/api/shared/province', function(res) {
            console.log(res);
            let { errno, data } = res;
            if (errno == 0) {
                console.log(data);
                let proData = data.map(v => {
                    return {
                        vulue: v.id,
                        label: v.name
                    }
                })
                console.log(proData);
                weui.picker(proData, {
                    onConfirm: function(result) {
                        console.log("确认触发", result[0]);
                        console.log(result[0].label);
                        console.log(dom.gete('#proTxt').innerHTML);
                        console.log(result[0].label);
                        console.dir(dom.gete('#proTxt'));
                        dom.gete('#proTxt').innerHTML = result[0].label;
                        proviceData = result[0];
                        console.log(proviceData);
                        console.log(proviceData.vulue);

                    },
                    title: '请选择省份'
                })
            }
        })
    })





    let cData;

    dom.addevent("#cityBtn", function() {
        console.log(proviceData);
        if (proviceData) {
            https.get('/api/shared/city?provinceId=' + proviceData.vulue, function(res) {
                console.log(res);
                let { errno, data } = res;
                if (errno == 0) {
                    console.log(data);
                    let cityData = data.map(v => {
                        return {
                            vulue: v.id,
                            label: v.name
                        }
                    })
                    console.log(cityData);
                    weui.picker(cityData, {
                        onConfirm: function(result) {
                            console.log("确认触发", result[0]);
                            console.log(result[0].label);
                            dom.gete('#cityTxt').innerHTML = result[0].label;
                            cData = result[0];
                            console.log(cData);
                        },
                        title: '请选择城市'
                    })
                }
            })
        } else {
            weui.alert('请先选择省份')
        }
    })

    dom.addevent("#signature", function() {
        let len = this.value.length;
        dom.gete("#word").innerHTML = len;
        if (len > 200) {
            dom.gete("#signature").value = this.value.slice(0, 200);
            dom.gete('#word').innerHTML = 200;
        }
    }, 'input')




    dom.addevent('#saveBtn', function() {
            https.post('/api/user/changeInfo', {
                    nickName: dom.gete('#user').value,
                    imgUrl,
                    gender: dom.gete('#sexTxt').innerHTML == '男' ? 0 : 1,
                    provinceId: proviceData.vulue,
                    cityId: cData.vulue,
                    birthday: dom.gete('#birTxt').innerHTML,
                    sign: dom.gete('#signature').value
                },
                function(res) {
                    console.log(res);
                    if (res.errno == 0) {
                        weui.toast("保存信息成功", 3000);
                        local.set("tk", res.data.token)
                        location.href = "my.html"
                    } else {
                        weui.toast("保存信息失败", 3000)
                    }
                })

        })
        //把本地数据渲染到网页中
    function rander() {
        //获取本地数据
        let localData = local.get('userdata');;
        let { nickName, gender, birthday, sign, provinceId, cityId } = localData;


        //头像
        dom.gete('.pic').src = 'http://fitness.h5.itsource.cn:3701' + localData.imgUrl;
        imgUrl = localData.imgUrl;

        dom.gete('#user').value = nickName; //昵称
        dom.gete('#sexTxt').innerHTML = gender == 1 ? '女' : '男'; //性别
        dom.gete('#birTxt').innerHTML = birthday; //生日
        dom.gete('#signature').value = sign; //个人简介

        getPos('#proTxt', provinceId, '#cityTxt', cityId)
        console.log(localData);

    }

    rander()

    function getPos(obj1, pD, obj2, cD) {

        https.get('/api/shared/province', function(res) {
            if (res.errno == 0) {
                let pData = res.data.filter(v => {
                    return v.id == pD;
                })

                dom.gete(obj1).innerHTML = pData[0].name;
                proviceData = {
                    label: pData[0].name,
                    value: pData[0].id
                }
                https.get('/api/shared/city?provinceId=' + pData[0].id, function(res) {
                    if (res.errno == 0) {
                        let ccData = res.data.filter(v => {
                            return v.id == cD;
                        })
                        dom.gete(obj2).innerHTML = ccData[0].name;

                        cData = {
                            label: ccData[0].name,
                            value: ccData[0].id
                        }
                    }
                })
            }
        })
    }
})